package com.lee.base.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.lee.common.page.PageView;
import com.lee.common.utils.Common;
import com.lee.system.model.ResourcesInfo;
import com.lee.system.service.ResourcesService;

@Controller
@RequestMapping("/base/")
public class BaseController {
	@Autowired
	private ResourcesService resourcesService;

	public PageView pageView = null;

	public PageView getPageView(String pageNow, String pageSize) {
		if (Common.isEmpty(pageNow)) {
			pageView = new PageView(1);
		} else {
			pageView = new PageView(Integer.parseInt(pageNow));
		}
		if (Common.isEmpty(pageSize)) {
			pageSize = "10";
		}
		pageView.setPageSize(Integer.parseInt(pageSize));
		return pageView;
	}

	public Map<String, Object> toFormMap(String pageNow, String pageSize) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", getPageView(pageNow, pageSize));
		return map;
	}

	@RequestMapping("toPage")
	public ModelAndView toAdd(HttpServletRequest request) {
		String page = request.getParameter("page");
		ModelAndView mav = new ModelAndView(page);
		return mav;
	}
	
	/**
	 * 
	 * findButton:(根据userid查找用户的按钮权限). <br/>
	 * TODO(这里描述这个方法适用条件 – 可选).<br/>
	 * TODO(这里描述这个方法的执行流程 – 可选).<br/>
	 * TODO(这里描述这个方法的使用方法 – 可选).<br/>
	 * TODO(这里描述这个方法的注意事项 – 可选).<br/>
	 *
	 * @author dell
	 * @param id
	 * @return
	 * @since JDK 1.7
	 */
	public List<ResourcesInfo> findButton(int id) {
		// 获取request
		Session session = SecurityUtils.getSubject().getSession();
		// user id
		int userId = (int) session.getAttribute("userSessionId");
		Map<String, Object> resQueryForm = new HashMap<String, Object>();
		resQueryForm.put("parentId", id);
		resQueryForm.put("userId", userId);
		resQueryForm.put("type", "2"); //type(1代表菜单，2代表按钮)
		List<ResourcesInfo> rse = resourcesService.findRes(resQueryForm);
		for (ResourcesInfo resourcesInfo : rse) {
			String description = resourcesInfo.getDescription();
			if(description!=null && !Common.isEmpty(description)){
				resourcesInfo.setDescription(Common.stringtohtml(description));
			}
		}
		return rse;
	}

	/**
	 * 获取页面传递的某一个参数值, <br/>
	 * <b>author：</b><br/>
	 * <b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsplanyuan</b><br/>
	 * <b>date：</b><br/>
	 * <b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2015-04-01</b><br/>
	 * <b>version：</b><br/>
	 * <b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp3.0v</b>
	 */
	public String getPara(String key) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request.getParameter(key);
	}

	/**
	 * 获取页面传递的某一个数组值, <br/>
	 * <b>author：</b><br/>
	 * <b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsplanyuan</b><br/>
	 * <b>date：</b><br/>
	 * <b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2015-04-01</b><br/>
	 * <b>version：</b><br/>
	 * <b>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp3.0v</b>
	 * 
	 * @return Class<T>
	 * @throws Exception
	 */
	public String[] getParaValues(String key) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request.getParameterValues(key);
	}
}
